<template>
    <div class="main">
        <div class="bj">
            <div class="denglu">
                <Row>
                    <Col span="12" :class="{'cur' : type=='登录','tap':true}">登录</Col>
                    <Col span="12" :class="{'cur' : type=='注册','tap':true}"></Col>
                </Row>
                <Form ref="forms" :model="formInline" :rules="ruleInline" inline>
                    <div class="inputItem">
                        <div class="userimg user"></div>
                        <FormItem prop="username">
                            <Input
                                type="text"
                                v-model="formInline.username"
                                placeholder="请输入手机号/电子邮件"
                            ></Input>
                        </FormItem>
                    </div>
                    <div class="inputItem">
                        <div class="userimg password"></div>
                        <FormItem prop="password">
                            <Input
                                type="password"
                                v-model="formInline.password"
                                placeholder=" 请输入密码"
                            ></Input>
                        </FormItem>
                    </div>
                    <div class="inputItem">
                        <Row>
                            <Col span="4">
                                <div class="userimg code"></div>
                            </Col>
                            <Col span="10">
                                <FormItem prop="code">
                                    <Input
                                        type="text"
                                        v-model="formInline.code"
                                        placeholder="请输入手机号"
                                    ></Input>
                                </FormItem>
                            </Col>
                            <Col span="10">
                                <img :src="'http://47.105.186.204:70/verify/' + code" height="40px" />
                            </Col>
                        </Row>
                    </div>
                    <!-- <Button type="primary" @click="$route.push({name:'findPass'})">找回</Button> -->
                </Form>
                <div class="button" @click="login('forms')">登录</div>
                <router-link to="/findPass">找回密码</router-link>
            </div>
        </div>
    </div>
</template>
<script>
import { get, post } from "../../http/http";
export default {
    data() {
        return {
            formInline: {
                username: "",
                password: "",
                code: ""
            },
            ruleInline: {
                username: [
                    {
                        required: true,
                        message: "请输入用户名",
                        trigger: "blur"
                    }
                ],
                password: [
                    {
                        required: true,
                        message: "请输入密码",
                        trigger: "blur"
                    },
                    {
                        type: "string",
                        min: 5,
                        message: "密码长度最少5位",
                        trigger: "blur"
                    }
                ],
                code: [
                    {
                        required: true,
                        message: "请输入验证码",
                        trigger: "blur"
                    }
                ]
            },
            type: "reg",
            code: Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000
        };
    },
    methods: {
        login(name) {
            this.$refs[name].validate(async valid => {
                if (valid) {
                    if (this.type == "reg") {
                    }
                    const data = await post("login", {
                        account: this.formInline.username,
                        password: this.formInline.password,
                        cerifyCode: this.code + "," + this.formInline.code
                    }).then(data => data);
                    if (data) {
                        if (data.code == 200) {
                            this.$store.state.UserToken = data.data.token;
                            this.$store.state.WeChat = data.data.user.WeChat;
                            this.$store.state.phone = data.data.user.phone;
                            this.$store.state.WeChat = data.data.user.WeChat;
                            this.$store.state.UserName =
                                data.data.user.userName;
                            this.$store.state.mail = data.data.user.mailbox;
                            this.$store.state.userId = data.data.user.id;

                            this.$store.state.department =
                                data.data.user.department;
                            this.$store.state.job = data.data.user.post;
                            this.$store.state.companyId =
                                data.data.user.companyId;

                            this.$store.state.permissions =
                                data.data.permissions;
                            this.$Message.success("登录成功 正在跳转转主页!");
                            this.$router.push({ path: `/index` });
                        } else if (data.code == -100) {
                            this.$Message.error("用户名密码错误!");
                        }
                    } else {
                        this.$store.UserToken = "";
                        this.$Message.error("账号或密码错误");
                        this.$router.push({ path: `/index` });
                    }
                } else {
                    this.$Message.error("请依据提示填写信息!");
                }
            });
        }
    }
};
</script>

<style lang="less" >
.main {
    background: url("../../../public/images/index/indexbj2.png") no-repeat;
    background-size: cover;
    height: 100%;
    .button{
        width: 100%;
        height: 50px;
        text-align: center;
        line-height: 50px;
        border-radius: 3px;
        background: #ebf5ff;
        margin-top: 20px;
        margin-bottom: 10px;
        box-shadow: 0px 6px 13px rgba(27,139,255, 20%);
        cursor: pointer;
    }
    .bj {
        background: url("../../../public/images/index/indexbj1.png") no-repeat;
        background-size: cover;
        height: 100%;
    }

    .userimg {

        margin-right: 10px;
        width: 40px;
        height: 40px;
        float: left;
        &.user {
            background: url("../../../public/images/index/user.png") no-repeat;
            background-size: 100%;
            background-position: center;
        }
        &.password {
            background: url("../../../public/images/index/password.png") no-repeat;
            background-size: 100%;
            background-position: center;
        }
        &.code {
            background: url("../../../public/images/index/360.png") no-repeat;
            background-size: 100%;
            background-position: center;
        }
    }
    .inputItem {
        border-bottom: 1px solid #e8e8e8;
        overflow: hidden;
        padding: 10px 0px;
        .ivu-form-item .ivu-form-item-required {
            width: 100%;
            border-bottom: 1px solid black;
            float: left;
        }
        .ivu-form-item {
            margin: 0px;
            margin-top: 3px;
        }

        .ivu-input {
            border: 0px;
            &:focus {
                box-shadow: none;
            }
        }
        .ivu-form-item-error-tip {
            font-size: 10px;
            position: absolute;
            top: 70%;
            left: 4px;
        }
    }

    .ivu-form-item-error .ivu-input {
        border: 0px;
    }
    .ivu-input-wrapper {
        width: 280px;
    }
    .denglu {
        position: absolute;
        height: 400px;
        width: 400px;
        padding: 25px;
        box-shadow: 0px 0px 5px rgba(0, 140, 255, 40%);
        top: 50%;
        left: 25%;
        -webkit-transform: translate(-50%, -50%);
        -moz-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
        .tap {
            height: 40px;
            border-bottom: 1px solid #dcdee2;
            color: #2d8cf0;
            font-weight: bold;
            font-size: 20px;
            margin-bottom: 10px;
            &cur {
                border-bottom: 1px solid #2d8cf0;
            }
        }
    }
}
</style>